package com.knockim.view.vicinity;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.TableRow;
import android.widget.TextView;

import com.knockim.R;
import com.knockim.cache.DrugCache;
import com.knockim.data.DataTest;
import com.knockim.model.Drug;

public class DrugProfileActivity extends Activity {
	
	HorizontalScrollView topScrollView;
	TextView retailPriceTextView = null;
	TextView distanceTextView = null;
	TextView nameTextView = null;
	TextView formatTextView = null;
	TextView typeTextView = null;
	TextView licenseTextView = null;
	TextView placeTextView = null;
	
	Button favoriteButton = null;
	
	Drug currentDrug = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.drug_profile);
		Bundle bundle = this.getIntent().getExtras();
		List<Drug> drugs = DrugCache.getDrugListCache();
		for(Drug drug : drugs){
			if(drug.getCode().equals(bundle.getString("code"))){
				currentDrug = drug;
			}
		}
		
		initView();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.drug_profile, menu);
		return true;
	}

	private int[] myImageIds = { R.drawable.imagedemo, R.drawable.imagedemo
			, R.drawable.imagedemo, R.drawable.imagedemo, R.drawable.imagedemo
			}; 
	private void initView() {
		
		favoriteButton = (Button) this.findViewById(R.id.drug_profile_button_favorite);
		favoriteButton.setText(currentDrug.isFavorite() ? R.string.drug_profile_button_unFavorite : R.string.drug_profile_button_favorite);
		favoriteButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				currentDrug.setFavorite(currentDrug.isFavorite() ? false : true);
				favoriteButton.setText(currentDrug.isFavorite() ? R.string.drug_profile_button_unFavorite : R.string.drug_profile_button_favorite);
			}
			
		});
		
		topScrollView = (HorizontalScrollView)findViewById(R.id.drug_profile_image_scrollview);
		TableRow row=(TableRow) this.findViewById(R.id.drug_profile_image_scrollview_row);
		//row.setOnTouchListener(this);
		//row.setOnFocusChangeListener(this);
		for (int i=0;i<myImageIds.length;i++){
		    ImageView imageView=new ImageView(this);
		    imageView.setImageResource(myImageIds[i]);
		    imageView.setPadding(3, 0, 3, 0);
		    //row.addView(imageView);
		    row.addView(imageView, 160, 160);
		}

		retailPriceTextView = (TextView) this.findViewById(R.id.drug_profile_label_retailPrice);
		distanceTextView = (TextView) this.findViewById(R.id.drug_profile_label_distance);
		nameTextView = (TextView) this.findViewById(R.id.drug_profile_label_name);
		formatTextView = (TextView) this.findViewById(R.id.drug_profile_label_format);
		typeTextView = (TextView) this.findViewById(R.id.drug_profile_label_type);
		licenseTextView = (TextView) this.findViewById(R.id.drug_profile_label_license);
		placeTextView = (TextView) this.findViewById(R.id.drug_profile_label_place);
		
		retailPriceTextView.setText("¥" + currentDrug.getRetailPrice());
		distanceTextView.setText(currentDrug.getDistance() * 100 + "米以内");
		nameTextView.setText(currentDrug.getName());
		formatTextView.setText(currentDrug.getFormat());
		typeTextView.setText(currentDrug.getType());
		licenseTextView.setText(currentDrug.getLicense());
		placeTextView.setText(currentDrug.getPlace());
	}
	
}
